"""
@author: shaoshuai
@file :bank_interface.py
@time: 2021/12/16 18:36
"""
from db import db_handler
from lib import common


# 根据不同的接口类型转入不同的日志对象
bank_logger = common.get_logger(log_type='bank')

# 提现金额接口（手续费5%）
def withdraw_interface(login_user,money):
    user_dic = db_handler.select(login_user)

    balance = int(user_dic.get('balance'))
    money = int(money)

    # 判断余额是否大于提现金额
    if balance >= money:
        # 开始扣钱
        balance -= (money * 1.05)

        user_dic['balance'] = balance

        # 记录流水
        flow = f'用户：[{login_user}] 提现金额：[{money}元成功，手续费为：[{money-float(money)}]'
        user_dic['flow'].append(flow)

        bank_logger.info(flow)

        # 保存数据
        db_handler.save(user_dic)
        return True, flow
    return False, '余额不足，提现不成功！'

# 还款接口
def repay_interface(login_user,money):
    user_dic = db_handler.select(login_user)
    # 取出用户金额
    balance = user_dic.get('balance')

    balance = int(balance)
    money = int(money)
    # 加钱操作
    balance += money
    user_dic['balance'] = balance

    # 记录流水
    flow = f'用户：[{login_user}] 还款：[{money}]成功！ 当前余额为：[{user_dic["balance"]}]'
    user_dic['flow'].append(flow)

    bank_logger.info(flow)
    # 保存数据
    db_handler.save(user_dic)

    return True, flow


# 转账功能接口
def transfer_interface(login_user,to_user,money):
    login_user_dic = db_handler.select(login_user)
    to_user_dic = db_handler.select(to_user)

    money = int(money)
    # 查看当前用户的金额
    if login_user_dic['balance'] >money:

        login_user_dic['balance'] -= money
        to_user_dic['balance'] += money

        # 当前用户流水记录
        login_user_flow = f'当前用户：[{login_user}] 向目标用户：[{to_user}] 转账：[{money}元] 成功！'
        login_user_dic['flow'].append(login_user_flow)

        # 目标用户流水记录
        to_user_flow = f'用户：[{to_user}] 接收转账：[{money}元] 成功！'
        to_user_dic['flow'].append(to_user_flow)


        # 保存数据
        db_handler.save(login_user_dic)
        db_handler.save(to_user_dic)

        msg = f'当前用户：[{login_user}] 向目标用户：[{to_user}] 转账：[{money}元] 成功！'
        bank_logger.info(msg)
        return True, msg

    msg = f'当前用户：[{login_user}],金额不足，转账失败！！'
    bank_logger.warn(msg)
    return False, msg

# 查看流水接口
def check_flow_interface(login_user):
    user_dic = db_handler.select(login_user)
    return user_dic.get('flow')

# 支付接口
def pay_interface(login_user,cost):
    # 查看用户的字典
    user_dic = db_handler.select(login_user)

    if user_dic['balance'] >= cost:
        # 开始扣钱
        user_dic['balance'] -= cost

        # 记录流水
        flow = f'用户消费金额：[{cost}$]'
        user_dic['flow'].append(flow)

        # 保存数据
        db_handler.save(user_dic)

        return True

    return False

# 清空购物车接口
def clean_pay_interface(login_user,cost):
    # 查看用户的字典
    user_dic = db_handler.select(login_user)

    if user_dic['balance'] >= cost:
        # 开始扣钱
        user_dic['balance'] -= cost

        # 记录流水
        flow = f'用户消费金额：[{cost}$]'
        user_dic['flow'].append(flow)

        # 把用户字典清空
        user_dic['shop_car'] = {}

        # 保存数据
        db_handler.save(user_dic)

        return True

    return False


